$(function () {
    jQuery.validator.addMethod("moneyVail", function (value) {
        return /^\s*[1-9]\d*\s*$/.test(value);
    }, "请输入正整数！");
    jQuery.validator.addMethod("billRiseVail", function (value) {
        return /^\s*[0-9a-zA-Z\u4e00-\u9fa5：，.()（）]{1,40}\s*$/.test(value);
    }, "请正确输入发票抬头！");
    jQuery.validator.addMethod("taxIdVail", function (value) {
        return /^\s*[0-9a-zA-Z]+\s*$/.test(value);
    }, "请正确输入税务登记号！");
    jQuery.validator.addMethod("bankNameVail", function (value) {
        return /^\s*[\u4E00-\u9FA5]*\s*$/.test(value);
    }, "请正确输入开户银行名称！");
    jQuery.validator.addMethod("bankAccountVail", function (value) {
        return /^(\s*\d{1,50}\s*)$/.test(value);
    }, "请正确输入开户账号！");
    //域名搜索规则验证
    jQuery.validator.addMethod("domainName", function (value) {
        return /^(\s*[0-9a-zA-Z\u4e00-\u9fa5]+([\-]?[0-9a-zA-Z\u4e00-\u9fa5])*(\.[a-zA-Z\u4e00-\u9fa5]{2,12}){0,2}\s*)?$/.test(value);
    }, "域名格式错误！");//非必填
    jQuery.validator.addMethod("RequiredDomainName", function (value) {
        return /^\s*[0-9a-zA-Z\u4e00-\u9fa5]+([\-]?[0-9a-zA-Z\u4e00-\u9fa5])*(\.[a-zA-Z\u4e00-\u9fa5]{2,12}){1,2}\s*$/.test(value);
    }, "域名格式错误！");//必填
    jQuery.validator.addMethod("keyWord", function (value) {
        return /^([-a-zA-Z0-9\u4e00-\u9fa5]+)?$/.test(value);
    }, "请输入合法的关键字！");
    jQuery.validator.addMethod("positiveInteger", function (value) {
        return /^(\d+)?$/.test(value);
    }, "请输入正整数！");
    jQuery.validator.addMethod("date", function (value) {
        return /^(((((19|20)\d{2})-(0?(1|[3-9])|1[012])-(0?[1-9]|[12]\d|30))|(((19|20)\d{2})-(0?[13578]|1[02])-31)|(((19|20)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))-0?2-29))+)?$/.test(value);
    }, "请规范输入日期格式！");
    jQuery.validator.addMethod("dnsKeyWord", function (value) {
        return /^([a-zA-Z0-9\-\.]+)?$/.test(value);
    }, "请正确输入DNS关键字！");
    jQuery.validator.addMethod("tplName", function (value) {
        if (value == "临时模板" || value == "隐私保护模板") return false;
        return /^[0-9a-zA-Z\u4e00-\u9fa5_-]{1,40}$/.test(value);
    }, "请正确输入模板名称！");
    jQuery.validator.addMethod("companyNameCn", function (value) {
        return /^([\u4e00-\u9fa5A-Za-z\.\s\,\(\)（）]){1,64}$/i.test(value);
    }, "请正确输入企业名称！");
    jQuery.validator.addMethod("companyNameEn", function (value) {
        return /^([a-z\.\s\,\(\)（）]{1,64})$/i.test(value);
    }, "请正确输入企业名称！");
    jQuery.validator.addMethod("personName", function (value) {
        return /^([\u4e00-\u9fa5\·]+\s?)+$/.test(value.trim());
    }, "姓名只能是汉字，分隔 · 用空格代替！");
    jQuery.validator.addMethod("englishName", function (value) {
        return /^([a-zA-Z]+\s?)+$/.test(value);
    }, "姓名只能是字母，分隔 · 用空格代替！");
    jQuery.validator.addMethod("address", function (value) {
        return /^(\s*[\s\S]{1,33}\s*)$/.test(value.trim());
    }, "请正确输入地址！");
    jQuery.validator.addMethod("addressEn", function (value) {
        return /^[-_A-Za-z0-9\s\,\.\#\&\(\)]{1,64}$/.test(value);
    }, "请正确输入地址！");
    jQuery.validator.addMethod("postCode", function (value) {
        return /^[0-9]\d{5}(?!\d)$/.test(value.trim());
    }, "请正确输入邮政编码！");
    jQuery.validator.addMethod("telephone", function (value) {
        return /^(0\d{9,11}|(1[35784]\d{9})|(400\d{7}))$/.test(value.trim());
    }, "请输入电话号码！");
    jQuery.validator.addMethod("phone", function (value) {
        return /^1[35784]\d{9}$/.test(value.trim());
    }, "请正确输入手机号！");
    jQuery.validator.addMethod("countryCode", function (value) {
        return /^((\+)?[0-9]{2,4})$/.test(value);
    }, "请输入国家代码！");
    jQuery.validator.addMethod("fax", function (value) {
        return /^\d{2,5}-\d{7,8}$/.test(value);
    }, "请正确输入传真号码！");
    jQuery.validator.addMethod("email", function (value) {
        return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(value);
    }, "请正确输入邮箱！");
    jQuery.validator.addMethod("cName", function (value) {
        return /^[\u4E00-\u9FA5]{2,5}(?:·[\u4E00-\u9FA5]{2,5})*$/.test(value);
    }, "请正确输入姓名！");
    jQuery.validator.addMethod("title", function (value) {
        return /^[\s|\S]{1,100}$/.test(value.trim());
    }, "至少一个字符，至多100个字符！");
    jQuery.validator.addMethod("detail", function (value) {
        return /^[\s|\S]{1,1000}$/.test(value.trim());
    }, "至少一个字符，至多1000个字符！");
    jQuery.validator.addMethod("money", function (value) {
        return /^(\s*?[1-9][\d]{0,9}|0)(\.[\d]{1,2}\s*?)?\s*?$/.test(value);
    }, "请正确输入金额，最多显示两位小数");
    jQuery.validator.addMethod("bankCardNumber", function (value) {
        return /^[1-9][0-9]{15,18}$/.test(value);
    }, "请正确输入银行卡号！");
    jQuery.validator.addMethod("textAreaDomainNames", function (value, ele) {
        var _val = value.trim();
        var arr = _val.split('\n');
        arr = arr.newUnique();
        var maxNumber = $(ele).data("maxNumber");
        if (arr.length > maxNumber) {
            return false;
        }
        var reg = /^([0-9a-zA-Z-\u4e00-\u9fa5]+\.[a-zA-Z\u4e00-\u9fa5]+)+$/;
        for (var i = 0; i < arr.length; i++) {
            arr[i] = arr[i].replace(/\s/g, '');
            if (!reg.test(arr[i])) {
                return false;
            }
        }
        return true;
    }, "输入的数据不正确！");
    jQuery.validator.addMethod("passportId", function (value, ele) {
        $(ele).parent().find("span").remove();
        var reg = /^[0-9]{1,9}$/;
        if (reg.test(value.trim())) {
            var isValid = false;
            $.ajax({
                async: false,
                url: "/ajax/user/info",
                type: "get",
                dataType: "json",
                data: {
                    userId: value.trim()
                },
                success: function (data) {
                    if (data && data.email) {
                        isValid = true;
                        $(ele).parent().append("<span class='color-succ'>" + data.email + "</span>");
                    }
                }
            });
            return isValid;
        } else {
            return false;
        }
    }, "请正确输入ID号!");
    jQuery.validator.addMethod("addressVail", function (value) {
        return /^(\s*[\s\S]{1,33}\s*)$/.test(value);
    }, "请正确输入地址！");
    jQuery.validator.addMethod("postCodeVail", function (value) {
        return /^\s*[0-9]\d{5}(?!\d)\s*$/.test(value);
    }, "请正确输入邮政编码！");
    jQuery.validator.addMethod("telVail", function (value) {
        return /^\s*(0\d{9,11}|(1[35784]\d{9}))\s*$/.test(value);
    }, "请正确输入联系电话！");
    jQuery.validator.addMethod("cNameVail", function (value) {
        return /^\s*([a-zA-Z\u4e00-\u9fa5\·]+\s?)+$/.test(value);
    }, "请正确输入姓名！");
    jQuery.validator.addMethod("addressVail", function (value) {
        return /^(\s*[\s\S]{1,33}\s*)$/.test(value);
    }, "请正确输入地址！");
    jQuery.validator.addMethod("billMoney", function (value) {
        var isValid =false;
        if ($(".bill-cash-hint").is(':visible') || $(".bill-suoqu-hint").is(':visible')) {
            isValid = false;
        }else {
            isValid =  true
        }
    }, "请正确输入地址！");

    // 添加模板校验
    $(".add-tpl-form").validate({
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        },
        rules: {
            templateName: {
                tplName: true
            },
            companyName: {
                companyNameCn: true
            },
            companyNameEn: {
                companyNameEn: true
            },
            //personName: {
            //	personName: true
            //},
            personNameEn: {
                englishName: true
            },
            address: {
                address: true
            },
            postCode: {
                postCode: true
            },
            phone: {
                telephone: true
            },
            countryCode: {
                countryCode: true
            },
            addressEn: {
                addressEn: true
            },
            provinceEn: {
                addressEn: true
            },
            cityEn: {
                addressEn: true
            },
            email: {
                email: true
            }
        },
        errorPlacement: function (error, element) {
            if (element.parent().find('#' + error.attr("id"))) {
                element.parent().find('#' + error.attr("id")).remove();
            }
            error.appendTo(element.parent());
        },
        success: function (label) {
            if (label.prev('#' + label.attr("id"))) {
                label.prev('#' + label.attr("id")).remove();
                label.parent().find('.valid-true').remove();
            }
            label.appendTo(label.parent());
            label.removeClass('error').addClass('valid-true');
        }
    });

    //添加地址校验
    $('.account-form').validate({
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        },
        rules: {
            address: {
                required: true,
                address: true
            },
            postcode: {
                required: true,
                postCode: true
            },
            city: {
                required: true
            },
            province: {
                required: true
            }
        },
        messages: {
            name: {
                required: '收件人姓名不能为空！',
                personName: '请正确输入姓名！'
            },
            address: {
                required: '街道地址不能为空！'
            },
            postcode: {
                required: '邮政编码不能为空！'
            },
            mobile: {
                required: '联系电话不能为空！'
            },
            city: {
                required: '请选择城市！'
            },
            province: {
                required: '请选择省市！'
            }
        },
        errorPlacement: function (error, element) {
            if (element.parent().find('#' + error.attr("id"))) {
                element.parent().find('#' + error.attr("id")).remove();
            }
            error.appendTo(element.parent());
        },
        success: function (label) {
            if (label.prev('#' + label.attr("id"))) {
                label.prev('#' + label.attr("id")).remove();
                label.parent().find('.valid-true').remove();
            }
            var text = label.parent().find('input[type="text"]');
            if (text.length != 0) {
                text.val(text.val().trim());
            }
            label.appendTo(label.parent());
            label.removeClass('error').addClass('valid-true');
            event.preventDefault();
            return false;
        },
        submitHandler: function () {
            event.preventDefault();
            $.ajax({
                type: "post",
                async: true,
                url: "/ajax/address/update",
                dataType: "json",
                data: $('.account-form').serialize(),
                success: function (data) {
                    //console.log(data);
                    if (data.isOk) {
                        $('#addr_province').html($('#province1').val());
                        $('#addr_city').html($('#city1').val());
                        $('#addr_area').html($('#area1').val());
                        $('#addr_address').html($('#address1').val());
                        $('#addr_postcode').html($('#postcode1').val());
                        layer.msg("操作成功", {time: 1000});
                    } else {
                        layer.msg("操作失败！", {time: 1000});
                    }
                    return false;
                }
            });
        }
    });

    $('.addressAdd-form').validate({
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        },
        rules: {
            name: {
                required: true,
                personName: true
            },
            address: {
                required: true,
                address: true
            },
            postcode: {
                required: true,
                postCode: true
            },
            mobile: {
                required: true,
                telephone: true
            }
        },
        messages: {
            name: {
                required: '收件人姓名不能为空！',
                personName: '请正确输入姓名！'
            },
            address: {
                required: '街道地址不能为空！'
            },
            postcode: {
                required: '邮政编码不能为空！'
            },
            mobile: {
                required: '联系电话不能为空！'
            }
        },
        errorPlacement: function (error, element) {
            if (element.parent().find('#' + error.attr("id"))) {
                element.parent().find('#' + error.attr("id")).remove();
            }
            error.appendTo(element.parent());
        },
        success: function (label) {
            if (label.prev('#' + label.attr("id"))) {
                label.prev('#' + label.attr("id")).remove();
                label.parent().find('.valid-true').remove();
            }
            var text = label.parent().find('input[type="text"]');
            text.val(text.val().trim());

            label.appendTo(label.parent());
            label.removeClass('error').addClass('valid-true');
        }
    });

    $('.address-form').validate({
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        },
        rules: {
            name: {
                required: true,
                personName: true
            },
            address: {
                required: true,
                address: true
            },
            postcode: {
                required: true,
                postCode: true
            },
            mobile: {
                required: true,
                telephone: true
            },
            city: {
                required: true
            },
            province: {
                required: true
            }
        },
        messages: {
            name: {
                required: '收件人姓名不能为空！',
                personName: '请正确输入姓名！'
            },
            address: {
                required: '街道地址不能为空！'
            },
            postcode: {
                required: '邮政编码不能为空！'
            },
            mobile: {
                required: '联系电话不能为空！'
            },
            city: {
                required: '请选择城市！'
            },
            province: {
                required: '请选择省市！'
            }
        },
        errorPlacement: function (error, element) {
            if (element.parent().find('#' + error.attr("id"))) {
                element.parent().find('#' + error.attr("id")).remove();
            }
            error.appendTo(element.parent());
        },
        success: function (label) {
            if (label.prev('#' + label.attr("id"))) {
                label.prev('#' + label.attr("id")).remove();
                label.parent().find('.valid-true').remove();
            }
            var text = label.parent().find('input[type="text"]');
            if (text.length != 0) {
                text.val(text.val().trim());
            }
            label.appendTo(label.parent());
            label.removeClass('error').addClass('valid-true');
            return false;
        },
        submitHandler: function () {
            var form = $('.address-form');
            $.ajax({
                type: "post",
                async: false,
                url: "/ajax/address/save",
                dataType: "json",
                data: form.serialize(),
                success: function (data) {
                    console.log(data);
                    if (data.isOk) {
                        addressList(data.id);
                        layer.msg("操作成功", {time: 1000});
                    } else {
                        layer.msg("操作失败！", {time: 1000});
                    }
                    return false;
                }
            });
        }
    });
});